# coding=utf-8
__author__ = 'st316'
'''
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.

Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)
'''


class Solution:
    # @return a list of lists of length 3, [[val1,val2,val3]]
    def threeSum(self, num):
        if len(num) == 0:
            return []
        r = set()
        s = set()
        for i in xrange(len(num)):
            if num[i] not in s:
                r = r.union(self.twoSum(num[:i] + num[i + 1:], -num[i]))
                s.add(num[i])
        return [list(x) for x in set(r)]

    def twoSum(self, num, target):
        d = {}
        r = set()
        for x in num:
            if target - x in d:
                r.add(self.sortList(x, target - x, -target))
            else:
                d[x] = True
        return r

    def sortList(self, a, b, c):
        if a > b:
            t = a
            a = b
            b = t
        if b > c:
            t = b
            b = c
            c = t
        if a > b:
            t = a
            a = b
            b = t
        return a, b, c

    def threeSumX(self, num):
        num.sort()

        result = []

        i = 0
        while i < len(num) - 2:
            if i > 0 and num[i] == num[i - 1]:
                i += 1
                continue

            j = i + 1
            k = len(num) - 1

            while j < k:
                if num[i] + num[j] + num[k] > 0:
                    k -= 1
                elif num[i] + num[j] + num[k] < 0:
                    j += 1
                else:
                    tmp = [num[i], num[j], num[k]]
                    result.append(tmp)

                    j += 1
                    k -= 1

                    while j < k and num[j] == num[j - 1]:
                        j += 1
                    while j < k and num[k] == num[k + 1]:
                        k -= 1
            i += 1

        return result


if __name__ == '__main__':
    s = Solution()
    import time

    cur1 = time.clock()
    print s.threeSum(
        [7, -1, 14, -12, -8, 7, 2, -15, 8, 8, -8, -14, -4, -5, 7, 9, 11, -4, -15, -6, 1, -14, 4, 3, 10, -5, 2, 1, 6, 11,
         2, -2, -5, -7, -6, 2, -15, 11, -6, 8, -4, 2, 1, -1, 4, -6, -15, 1, 5, -15, 10, 14, 9, -8, -6, 4, -6, 11, 12,
         -15, 7, -1, -9, 9, -1, 0, -4, -1, -12, -2, 14, -9, 7, 0, -3, -4, 1, -2, 12, 14, -10, 0, 5, 14, -1, 14, 3, 8,
         10, -8, 8, -5, -2, 6, -11, 12, 13, -7, -12, 8, 6, -13, 14, -2, -5, -11, 1, 3, -6])
    cur2 = time.clock()
    print s.threeSum(
        [7, -1, 14, -12, -8, 7, 2, -15, 8, 8, -8, -14, -4, -5, 7, 9, 11, -4, -15, -6, 1, -14, 4, 3, 10, -5, 2, 1, 6, 11,
         2, -2, -5, -7, -6, 2, -15, 11, -6, 8, -4, 2, 1, -1, 4, -6, -15, 1, 5, -15, 10, 14, 9, -8, -6, 4, -6, 11, 12,
         -15, 7, -1, -9, 9, -1, 0, -4, -1, -12, -2, 14, -9, 7, 0, -3, -4, 1, -2, 12, 14, -10, 0, 5, 14, -1, 14, 3, 8,
         10, -8, 8, -5, -2, 6, -11, 12, 13, -7, -12, 8, 6, -13, 14, -2, -5, -11, 1, 3, -6])
    cur3 = time.clock()
    print cur2 - cur1
    print cur3 - cur2